function [out]=Payoff_Conservation_EFvalue_v1(x)

global theta  numpatches Dii   domain_suit_hab max_age age_mature...
    age_legal  Wij Rmax alpha1 delta Kvirgin  pr max_simulation_length ...
       Fsum  hide0_show1_figures K Bi_legal_fish  beta_i_initial ...
       Bij_initial TargetBiomass CRgoal theta_percent FleetProfit_Fsum_opt...
       x0_Fsumguess LB UB MPA_locs Fsum_UB_prop_wrt_sumFishingHab...
       Payoff_Conservation_EF_alpha_dynamic...
       NCI_MPA_locs...
       NPVon discounted_value_iy discounted_value_ijy

% Payoff_Conservation_EF_alpha=0.1; %comment out if running in loop

% (input from Heuristic)
% MPA_locs=x'; %locs that are fished (0) or MPAs (1) -  this is old and does not account for NCI MPAs
%below accounts for NCI MPAs
tmp=NCI_MPA_locs; %set vector just with NCI MPAs
tmp(NCI_MPA_locs==0)=x'; %fill itn the new MPAs by heuristic
MPA_locs=tmp;

% Find optimal Fsum that maximizes payoff
%fmincon==================================================
% warning off
% options_OptFsum=optimset('Display','off');
% [Fsum_opt NEG_FleetProfit_Fsum_opt flag]=fmincon(@OptFsum,x0,[],[],[],[],LB,UB,[],options_OptFsum);
% warning on
%npsol==================================================
t2start = tic;
% options_fmincon_OptFsum=optimset('Algorithm','interior-point','Display','off');
    TolX_value=1e-4*numpatches; %Set tolerance for Fsum search. 1e-4 is the default TolX. 
    TolFun_value=TolX_value;
options_fmincon_OptFsum=optimset('Algorithm','interior-point','Display','off','TolX',TolX_value,'TolFun',TolFun_value);
    % We have so many patches and thus Fsum is so large (and fracional changes in Fsum don't matter much) so let's make Fsum larger
    
    
prob.obj = 'OptFsum';
prob.guess = [x0_Fsumguess];
prob.lo = [LB];
prob.up = [Fsum_UB_prop_wrt_sumFishingHab*sum(domain_suit_hab(MPA_locs==0))]; %[UB];
% [NEG_FleetProfit_Fsum_opt,Fsum_out, g, hess, flag, iter]=npsol(prob);  % OPTIMIZATION
[Fsum_out,NEG_FleetProfit_Fsum_opt,flag,output]=fmincon(@OptFsum,prob.guess,[],[],[],[],prob.lo,prob.up,[],options_fmincon_OptFsum); %v7
%=========================================================
% disp(['#MPAs=',num2str(length(find(MPA_locs==1))),'; Fsum_out=',num2str(Fsum_out),'; x0_Fsumguess-Fsum_out=',num2str(x0_Fsumguess-Fsum_out),'; took ',num2str(toc(t2start)),' seconds'])
if Fsum_out==LB
    disp('WARNING: Fsum_out==LB')
end
if Fsum_out==UB
    disp('WARNING: Fsum_out==UB')
end

x0_Fsumguess=Fsum_out;
bound_range=2; %Fsum should be close to previous Fsum bc only +/- one MPA
LB=max([(x0_Fsumguess-bound_range) 0]);
UB=x0_Fsumguess+bound_range;

Fsum=Fsum_out;

%run output
MOPmodel2

%scaled payoff
tmp1=sum(Profit_iy,1);
tmp2=tmp1(end); 
payoff_scaled=tmp2/FleetProfit_Fsum_opt; 

%scaled conservation
tmp1=sum(sum(Bijy(:,:,end)));
conservation_scaled=tmp1/Kvirgin;

out_display=-sum(Payoff_Conservation_EF_alpha_dynamic*payoff_scaled + (1-Payoff_Conservation_EF_alpha_dynamic)*conservation_scaled);
% disp(['Payoff_Conservation_EF_alpha=',num2str(Payoff_Conservation_EF_alpha),'; out (goal is NEG ONE)=',num2str(out_display),]);
out=out_display;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% % out=-sum((1-EF_E_W_weight)*(energyEF/max_energy_profit) + EF_E_W_weight*(Whale_wrt_Energy_P1P2_Energy_Whale_EF/Virgin_Whale_wrt_energy_P1P2));
% 
% 
% 
% 
% 
% %%%%
% % x = Fsum
% 
% % global theta Pmax numpatches Dii Mii Nij_initial domain_frac_suit_hab max_age age_mature...
% %     age_legal age_move Wij Rmax alpha1 delta S_Y_weightvalue S_P_weightvalue Kvirgin Ymax pr...
% %     tolerance Optimize_0nonspatial_1spatial max_simulation_length whalew_density whale_value_virgin...
% %     W_F_local W_F_system W_Y_weightvalue y Bi_legal Fsum Fi hide0_show1_figures...
% %     RNi Si K Bi_legal_fish energy_binary_i energy_i rocky_habitat_per_squaremeter_energyarea...
% %     M MaxCellAreaHabitat beta_i_initial energy_locs species_habitat_i species_lobster1_finfish2...
% %     energy_binary_i_P1P2_repmat Value_1ALL_2onlyP1P2 footprint_per_squaremeter_energyarea Fsum_opt LB UB...
% %      energy_profit_i discount_lifetime divisor Profit_y whalew_density_P1P2 Construction_time turbine_lifetime...
% %      energy_locs EF_E_FW_weight Profit_equil_wrt_Energy_P1P2 max_energy_profit EF_E_W_weight Virgin_Whale_wrt_energy_P1P2
% 
% % EF_E_W_weight=0.1; 
%  
% energy_locs=zeros(numpatches,1);
% energy_locs(energy_binary_i==1)=x;
% % energy_locs=logical(energy_locs);
% 
% energyEF=sum(energy_profit_i.*energy_locs);
% 
% 
% % % % % %%%%%%%%%%%%%%
% % % % % % warning off
% % % % % % options_OptFsum=optimset('Display','off');
% % % % % % [Fsum_opt maxFleetProfit flag]=fmincon(@OptFsum,x0,[],[],[],[],LB,UB,[],options_OptFsum);
% % % % % % warning on
% % % % % %%%%%%%%%%%%%%
% % x0=Fsum_opt; %set guess at previous output
% % path('\\tsclient\H\Data & Models\npsol;',path); %This enables npsol
% % prob.guess = [x0];
% % prob.obj = 'OptFsum';
% % prob.lo = [LB];
% % prob.up = [UB];
% % [maxFleetProfit, Fsum_opt, g, hess, flag, iter]=npsol(prob);  % OPTIMIZATION
% %%%%%%%%%%%%%
% % disp(['Fsum_opt = ',num2str(Fsum_opt),'; maxFleetProfit = ',num2str(-maxFleetProfit),'; Exit flag = ',num2str(flag),])
% % Fsum=Fsum_opt;
% MOPmodel2
% %record values,
% % flag_out(end,1)=flag;
% % energy_locs_record(end,1,:)=energy_locs;
% % energy_wrt_Energy(end,1)=sum(energy_profit_i.*energy_locs);
% 
% % Fsum_wrt_Energy(end,1)=Fsum;
% % Fi_wrt_Energy(end,1,:)=Fi;
% % %full domain
% % Profit_y=sum(Profit_iy,1);
% % Profit_i=Profit_iy(:,end);
% % if species_lobster1_finfish2==1 %lobster fishing buffer during construciton phase
% %     Profit_i_construction=Profit_i.*(energy_locs==0);
% %     Profit_equil_wrt_Energy(end,1)=sum((sum([repmat(Profit_i_construction,1,Construction_time) repmat(Profit_i,1,turbine_lifetime)],1)).*discount_lifetime);
% %     Profit_equil_wrt_Energy(end,1)=Profit_equil_wrt_Energy(end,1)/divisor;
% % elseif species_lobster1_finfish2==2
% %     Profit_equil_wrt_Energy(end,1)=sum(Profit_y(end)*discount_lifetime);
% %     Profit_equil_wrt_Energy(end,1)=Profit_equil_wrt_Energy(end,1)/divisor;
% % end
% %P1 and P2
% Profit_iy=Profit_iy.*energy_binary_i_P1P2_repmat; %ONLY VALUE IN P1 AND P2
% Profit_y=sum(Profit_iy,1);
% Profit_i=Profit_iy(:,end);
% if species_lobster1_finfish2==1 %lobster fishing buffer during construciton phase
%     Profit_i_construction=Profit_i.*(energy_locs==0);
%     Profit_equil_wrt_Energy_P1P2_Energy_Fishery_EF=sum((sum([repmat(Profit_i_construction,1,Construction_time) repmat(Profit_i,1,turbine_lifetime)],1)).*discount_lifetime);
% %     Profit_equil_wrt_Energy_P1P2_Energy_Fishery_EF=Profit_equil_wrt_Energy_P1P2_Energy_Fishery_EF/divisor;
% elseif species_lobster1_finfish2==2
%     Profit_equil_wrt_Energy_P1P2_Energy_Fishery_EF=sum(Profit_y(end).*discount_lifetime);
% %     Profit_equil_wrt_Energy_P1P2_Energy_Fishery_EF=Profit_equil_wrt_Energy_P1P2_Energy_Fishery_EF/divisor;
% end
% 
% % %full domain
% % Whale_wrt_energy_i=sum(sum([repmat((whalew_density.*(energy_locs==0)),1,Construction_time) repmat(whalew_density,1,turbine_lifetime)],1).*discount_lifetime); %discounted whale value
% % Whale_wrt_energy_i=Whale_wrt_energy_i./divisor;
% % if species_lobster1_finfish2==1
% %     Whale_wrt_Energy_Fishing=Whale_wrt_energy_i-Fsum*W_F_system*Whale_wrt_energy_i; %further reduction of whales due to fishing
% % elseif species_lobster1_finfish2==2
% %     Whale_wrt_Energy_Fishing=sum(Whale_wrt_energy_i);
% % end
% % Whale_wrt_Energy(end,1)= Whale_wrt_Energy_Fishing;
% %P1P2
% 
% % tmp4=(whalew_density_P1P2.*(energy_locs==0));
% % tmp3=repmat(whalew_density_P1P2,1,turbine_lifetime);
% % tmp2=[repmat(tmp4,1,Construction_time) tmp3];
% % tmp1=sum(tmp2,1).*discount_lifetime;
% % Whale_wrt_energy_i=sum(tmp1); %discounted whale value
% Whale_wrt_energy_i=sum(sum([repmat((whalew_density_P1P2.*(energy_locs==0)),1,Construction_time) repmat(whalew_density_P1P2,1,turbine_lifetime)],1).*discount_lifetime); %discounted whale value
% Whale_wrt_energy_i=Whale_wrt_energy_i./divisor;
% % Profit_iy=Profit_iy.*energy_binary_i_P1P2_repmat; %ONLY VALUE IN P1 AND P2
% if species_lobster1_finfish2==1
%     Whale_wrt_Energy_Fishing=Whale_wrt_energy_i-Fsum*W_F_system*Whale_wrt_energy_i;
% elseif species_lobster1_finfish2==2
%     Whale_wrt_Energy_Fishing=sum(Whale_wrt_energy_i);
% end
% Whale_wrt_Energy_P1P2_Energy_Whale_EF= Whale_wrt_Energy_Fishing;
% 
% 
% % tmp1=sum(sum([repmat((whalew_density_P1P2.*(energy_locs==0)),1,Construction_time) repmat(whalew_density_P1P2,1,turbine_lifetime)],1).*discount_lifetime); %discounted whale value
% % tmp1=tmp1./divisor;
% % if species_lobster1_finfish2==1
% %     MaxWhale_wrt_NoEnergy_YesFishing=tmp1-Fsum_opt*W_F_system*tmp1;
% % elseif species_lobster1_finfish2==2
% %     MaxWhale_wrt_NoEnergy_YesFishing=sum(tmp1);
% % end
% % MaxWhale_wrt_NoEnergy_YesFishing
% 
% 
% out=-sum((1-EF_E_W_weight)*(energyEF/max_energy_profit) + EF_E_W_weight*(Whale_wrt_Energy_P1P2_Energy_Whale_EF/Virgin_Whale_wrt_energy_P1P2));
